﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Linq.Dynamic;using System.Text;
using System.Data.Linq;
using System.Transactions;
using LinqToSqlExtensions;using System.ServiceModel;
using System.Collections;
using System.Collections.Generic;
using CardsServer.DAL;

namespace CardsServer.WCF
{
	public partial interface IService
{
[OperationContract]
string Sale_Add(Sale model ,int ACID);


[OperationContract]
string Sale_Update(Sale model ,int ACID);


[OperationContract]
string SaleByinvoiceid_Delete(int invoiceid,int ACID);


[OperationContract]
Sale SaleByinvoiceid_Get(int invoiceid,int ACID );






}
public partial class Service : IService
{
#region  添加Sale表数据
/// <summary>
/// 添加Sale
/// </summary>
/// <param name="model">Sale实体</param>
/// <returns>空或错误</returns>
public string Sale_Add(Sale model,int ACID)
{
if(model.invoiceid==null )
{
return "『销售单ID』不能为空！";
}
if(string.IsNullOrEmpty(model.shopcode))
{
return "『分支机构编号』不能为空！";
}
if(model.billtypeid==null )
{
return "『单据类型ID』不能为空！";
}
if(model.disc==null )
{
return "『折扣』不能为空！";
}
if(model.totalrcvd==null )
{
return "『已收金额』不能为空！";
}
if(model.totalamt==null )
{
return "『总金额』不能为空！";
}
if(model.opID==null || model.opID.Equals(Guid.Empty))
{
return "『操作员ID』不能为空！";
}
if(model.billdate==null )
{
return "『单据日期』不能为空！";
}
if(string.IsNullOrEmpty(model.code))
{
return "『单据编号』不能为空！";
}
if(model.ClientID==null || model.ClientID.Equals(Guid.Empty))
{
return "『客户ID』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.Sale.Count(p=>p.invoiceid==model.invoiceid)>0)
{
return "主键已存在！";
}
ctx.Sale.InsertOnSubmit(model);
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 添加失败!错误详细信息：" + ex.Message;
}
}
}
#endregion

#region  添加Sale表数据事物用重构
/// <summary>
/// 添加Sale事物用重构
/// </summary>
/// <param name="model">Sale实体</param>
/// <returns>空或错误</returns>
private string Sale_Add(Sale model,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
if(model.invoiceid==null )
{
return "『销售单ID』不能为空！";
}
if(string.IsNullOrEmpty(model.shopcode))
{
return "『分支机构编号』不能为空！";
}
if(model.billtypeid==null )
{
return "『单据类型ID』不能为空！";
}
if(model.disc==null )
{
return "『折扣』不能为空！";
}
if(model.totalrcvd==null )
{
return "『已收金额』不能为空！";
}
if(model.totalamt==null )
{
return "『总金额』不能为空！";
}
if(model.opID==null || model.opID.Equals(Guid.Empty))
{
return "『操作员ID』不能为空！";
}
if(model.billdate==null )
{
return "『单据日期』不能为空！";
}
if(string.IsNullOrEmpty(model.code))
{
return "『单据编号』不能为空！";
}
if(model.ClientID==null || model.ClientID.Equals(Guid.Empty))
{
return "『客户ID』不能为空！";
}
if(ctx.Sale.Count(p=>p.invoiceid==model.invoiceid)>0)
{
return "主键已存在！";
}
ctx.Sale.InsertOnSubmit(model);
return string.Empty;
}
#endregion



#region  修改Sale表数据
/// <summary>
/// 修改Sale
/// </summary>
/// <param name="model">Sale实体</param>
/// <returns>空或错误</returns>
public string Sale_Update(Sale model,int ACID)
{
if(model.invoiceid==null )
{
return "『销售单ID』不能为空！";
}
if(string.IsNullOrEmpty(model.shopcode))
{
return "『分支机构编号』不能为空！";
}
if(model.billtypeid==null )
{
return "『单据类型ID』不能为空！";
}
if(model.disc==null )
{
return "『折扣』不能为空！";
}
if(model.totalrcvd==null )
{
return "『已收金额』不能为空！";
}
if(model.totalamt==null )
{
return "『总金额』不能为空！";
}
if(model.opID==null || model.opID.Equals(Guid.Empty))
{
return "『操作员ID』不能为空！";
}
if(model.billdate==null )
{
return "『单据日期』不能为空！";
}
if(string.IsNullOrEmpty(model.code))
{
return "『单据编号』不能为空！";
}
if(model.ClientID==null || model.ClientID.Equals(Guid.Empty))
{
return "『客户ID』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.Sale.Count(p=>p.invoiceid==model.invoiceid)!= 1)
{
return "未找到指定记录或记录不唯一！";
}
Sale Salemodel=ctx.Sale.Single(p => p.invoiceid==model.invoiceid);
if(model.invoiceid!=null )
{
Salemodel.invoiceid=model.invoiceid;
}
if(model.ClientID!=null || model.ClientID.Equals(Guid.Empty))
{
Salemodel.ClientID=model.ClientID;
}
if(!string.IsNullOrEmpty(model.DepCode))
{
Salemodel.DepCode=model.DepCode;
}
if(model.exemanid!=null || model.exemanid.Equals(Guid.Empty))
{
Salemodel.exemanid=model.exemanid;
}
if(!string.IsNullOrEmpty(model.shopcode))
{
Salemodel.shopcode=model.shopcode;
}
if(model.billtypeid!=null )
{
Salemodel.billtypeid=model.billtypeid;
}
if(model.ContactID!=null )
{
Salemodel.ContactID=model.ContactID;
}
if(!string.IsNullOrEmpty(model.code))
{
Salemodel.code=model.code;
}
if(model.billdate!=null )
{
Salemodel.billdate=model.billdate;
}
if(model.opID!=null || model.opID.Equals(Guid.Empty))
{
Salemodel.opID=model.opID;
}
if(!string.IsNullOrEmpty(model.memo))
{
Salemodel.memo=model.memo;
}
if(model.termdays!=null )
{
Salemodel.termdays=model.termdays;
}
if(model.totalamt!=null )
{
Salemodel.totalamt=model.totalamt;
}
if(model.totalrcvd!=null )
{
Salemodel.totalrcvd=model.totalrcvd;
}
if(model.disc!=null )
{
Salemodel.disc=model.disc;
}
if(!string.IsNullOrEmpty(model.userdef1))
{
Salemodel.userdef1=model.userdef1;
}
if(!string.IsNullOrEmpty(model.userdef2))
{
Salemodel.userdef2=model.userdef2;
}
if(!string.IsNullOrEmpty(model.userdef3))
{
Salemodel.userdef3=model.userdef3;
}
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 修改失败!错误详细信息：" + ex.Message;
}
}
}
#endregion



#region  删除Sale表Byinvoiceid数据
/// <summary>
/// 删除SaleByinvoiceid
/// </summary>
/// <param name="invoiceid">销售单ID</param>
/// <returns>空或错误</returns>
public string SaleByinvoiceid_Delete(int invoiceid,int ACID)
{
if(invoiceid==null )
{
return "『销售单ID』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.Sale.Count(p=>p.invoiceid==invoiceid)!=1)
{
return null;
}
Sale model=ctx.Sale.Single(p => p.invoiceid== invoiceid);
ctx.Sale.DeleteOnSubmit(model);
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 删除失败!错误详细信息：" + ex.Message;
}
}
}
#endregion

#region  删除Sale表Byinvoiceid数据[事物用重构]
/// <summary>
/// 删除SaleByinvoiceid[事物用重构]
/// </summary>
/// <param name="invoiceid">销售单ID</param>
/// <returns>空或错误</returns>
private string SaleByinvoiceid_Delete(int invoiceid,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
if(invoiceid==null )
{
return "『销售单ID』不能为空！";
}
if(ctx.Sale.Count(p=>p.invoiceid==invoiceid)!=1)
{
return null;
}
Sale model=ctx.Sale.Single(p => p.invoiceid== invoiceid);
ctx.Sale.DeleteOnSubmit(model);
return string.Empty;
}
#endregion



#region  获取Sale表Byinvoiceid数据
/// <summary>
/// 获取SaleByinvoiceid
/// </summary>
/// <param name="invoiceid">销售单ID</param>
/// <returns>当前表</returns>
public Sale SaleByinvoiceid_Get(int invoiceid,int ACID)
{
if(invoiceid==null )
{
return null;
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.Sale.Count(p=>p.invoiceid==invoiceid)!=1)
{
return null;
}
return ctx.Sale.SingleOrDefault(p=>p.invoiceid==invoiceid);
}
catch (Exception ex)
{
return null;
}
}
}
#endregion



#region  获取Sale表数据
/// <summary>
/// 获取Sale
/// </summary>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<Sale></returns>
public List<Sale> Sale_Get(Expression<Func<Sale, bool>> predicate , string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.Sale.ToList();
}
 else 
{
return ctx.Sale.OrderBy(sortBy).ToList();
}
}
else
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.Sale.Where(predicate).ToList();
}
 else 
{
return ctx.Sale.Where(predicate).OrderBy(sortBy).ToList();
}
}
}
catch (Exception ex)
{
return null;
}
}
}
#endregion




#region  获取Sale表数据计数
/// <summary>
/// 获取Sale表数据计数
/// </summary>
/// <returns>List<Sale></returns>
public int Sale_GetCount(Expression<Func<Sale, bool>> predicate,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
return ctx.Sale.Count();

}
 else
{
return ctx.Sale.Where(predicate).Count();
}
}
catch 
{
return 0;
}
}
}
#endregion




#region  获取Sale分页表数据
/// <summary>
/// 获取Sale分页表数据
/// </summary>
/// <param name="pageIndex">当前页</param>
/// <param name="pageSize">页大小</param>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<Sale></returns>
public List<Sale> Sale_Getpage(int pageIndex, int pageSize,Expression<Func<Sale, bool>> predicate ,string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.Sale.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
 else 
{
return ctx.Sale.OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
}
else
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.Sale.Where(predicate).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
 else 
{
return ctx.Sale.Where(predicate).OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
}
}
catch (Exception ex)
{
return null;
}
}
}
#endregion


#region  批量修改Sale表数据
/// <summary>
/// 批量修改Sale
/// </summary>
/// <param name="predicate">Expression<Func<Sale, bool>></param> 查找表达式树
/// <param name="updater">Expression<Func<Sale,Sale>> 更新表达式树</param>
/// <param name="returns">影响记录数</param>
public int Sale_batchUpdate(Expression<Func<Sale, bool>> predicate,Expression<Func<Sale, Sale>> updater,int ACID)
{
using (TransactionScope ts=new TransactionScope())
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
int ok = ctx.Sale.Update(predicate, updater);
ts.Complete();
 return ok;
}
catch (Exception ex)
{
return 0;
}
}
}
}
#endregion


#region  批量修改Sale表数据事物用重构
/// <summary>
/// 批量修改Sale事物用重构
/// </summary>
/// <param name="predicate">Expression<Func<Sale, bool>></param> 查找表达式树
/// <param name="updater">Expression<Func<Sale,Sale>> 更新表达式树</param>
/// <param name="ctx">数据上下文</param>
/// <param name="returns">影响记录数</param>
private int Sale_batchUpdate(Expression<Func<Sale, bool>> predicate,Expression<Func<Sale, Sale>> updater,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
int ok = ctx.Sale.Update(predicate, updater);
return ok;
}
#endregion


#region  批量删除Sale表数据
/// <summary>
/// 批量删除Sale
/// </summary>
/// <param name="predicate">Expression<Func<Sale, bool>></param> 查找表达式树
/// <param name="returns">影响记录数</param>
public int Sale_batchDel(Expression<Func<Sale, bool>> predicate,int ACID)
{
using (TransactionScope ts=new TransactionScope())
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
int ok = ctx.Sale.Delete(predicate);
ts.Complete();
 return ok;
}
catch (Exception ex)
{
return 0;
}
}
}
}
#endregion


#region  批量删除Sale表数据事物用重构
/// <summary>
/// 批量删除Sale事物用重构
/// </summary>
/// <param name="predicate">Expression<Func<Sale, bool>></param> 查找表达式树
/// <param name="ctx">数据上下文</param>
/// <param name="returns">影响记录数</param>
private int Sale_batchDel(Expression<Func<Sale, bool>> predicate,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
int ok = ctx.Sale.Delete(predicate);
return ok;
}
#endregion


}
}

